import {observable, action, toJS} from 'mobx';
import * as API from '../apis/robot';
import {Modal, message} from 'antd';
import { WeaTableNew, WeaForm } from 'comsMobx';
const {TableStore} = WeaTableNew;
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

class WeesoRobotStore {
  @observable form = new WeaForm();
  @observable createForm = new WeaForm();
  @observable tableStore = new TableStore();

  @observable status = {
    showSearchAd: false,
    conditioninfo: [], //高级搜索
    alertPage: false,
  }

  @observable dialogStatus = {
    createOrEdit: 0, //0：新建 1：
    editId: '', // 编辑id
    canClick: true, //控制新建、编辑不能同一时刻多次点击
    showDialog: false, //新建、编辑弹框
    dialogLoading: false,
    saveLoading: false, //保存
    createFields: [],
  }

  @action

  doInit = () => {
    this.getList();
    this.getCondition();
  }

  setState = (params = {}) => {
    let status = {...this.status};
    Object.keys(params).forEach(key => {
      status[key] = params[key];
    });
    this.status = status;
  }

  setDialogState = (params = {}) => {
    let dialogStatus = {...this.dialogStatus};
    Object.keys(params).forEach(key => {
      dialogStatus[key] = params[key];
    });
    this.dialogStatus = dialogStatus;
  }

  //指令列表
  getList = (isOnChange = false) => {
    let params = this.form && this.form.getFormParams() || {};
    API.getList(params).then(result => {
      if(result.ret == 'noright'){
        this.status.alertPage = true;
      }else {
        this.status.alertPage = false;
        isOnChange ? this.tableStore.getDatas(result.sessionkey, 1) :this.tableStore.getDatas(result.sessionkey)
      }
    })
  }

  //高级搜索条件
  getCondition = () => {
    API.getCondition().then(result => {
      this.form = new WeaForm();
      this.form.initFormFields(result.condition);
      this.status = {...this.status, conditioninfo: result.condition}
    })
  }

  //添加修改数据展示
  getFields = (params) => {
    this.dialogStatus.canClick = false;
    API.getFields(params).then(result => {
      if(result.ret == 'noright'){
        this.dialogStatus.alertPage = true;
      }else {
        this.dialogStatus.alertPage = false;
        this.createForm = new WeaForm();
        this.createForm.initFormFields(result.fields);
        window.e9ChangedFormFieldKey = 'bakkdakekoafkoew';
        this.dialogStatus = {...this.dialogStatus, createFields: result.fields, showDialog: true, canClick: true}
      }
    })
  }

  // 添加
  addRobot = (params) => {
    this.dialogStatus.saveLoading = true;
    API.addRobot(params).then(result => {
      this.dialogStatus = {...this.dialogStatus, saveLoading: false, showDialog: false};
      this.doCommonFunc(result);
    })
  }

  // 修改
  editRobot = (params) => {
    this.dialogStatus.saveLoading = true;
    API.editRobot(params).then(result => {
      this.dialogStatus = {...this.dialogStatus, saveLoading: false, showDialog: false};
      this.doCommonFunc(result);
    })
  }

  // 删除
  deleteRobot = (params) => {
    API.deleteRobot(params).then(result => {
      this.doCommonFunc(result);
    })
  }

  // 启用/禁用
  openRobot = (params) => {
    API.openRobot(params).then(result => {
      this.doCommonFunc(result);
    })
  }

  // 创建索引
  createRobotIndex = (params) => {
    API.createRobotIndex(params).then(result => {
      if(result.ret == 'true'){
        message.success(getLabel(83885,'操作成功！'));
      }else {
        message.error(getLabel(83912,'操作失败！'));
      }
    })
  }

  doCommonFunc = (result) => {
    if(result.ret == 'true'){
      message.success(getLabel(83885,'操作成功！'));
      this.getList();
    }else {
      message.error(result.msg ? result.msg : getLabel(83912,'操作失败！'));
    }
  }

}

const weesoRobotStore = new WeesoRobotStore();
export default weesoRobotStore;

